package com.jwm.client;

import com.jwm.client.soft.*;
import com.jwm.model.*;
import com.jwm.model.ServiceVo;
import com.jwm.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import oshi.util.Util;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 崔洁
 */
@Component
public class AfterRunner implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(AfterRunner.class);
    @Resource
    private ClientConfig clientConfig;
    @Resource
    private NginxConfigEditor nginxConfigEditor;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Util.sleep(1000);
        logger.warn("开始初始化");
        if (clientConfig.getTimeHeartInterval() > 0) {
            logger.warn("初始化查询，共7项");
            ExecuteCommand exe = new ExecuteCommand();
            logger.warn("1、获取电脑信息");
            Computer computer = new Computer();
            computer.copyTo();
            logger.warn("1、获取电脑信息成功");
            logger.warn("2、获取磁盘信息");
            SysFiles sysFiles = new SysFiles();
            sysFiles.copyTo();
            logger.warn("2、获取磁盘信息成功");
            logger.warn("3、获取tomcat信息");
            for (App appVo : clientConfig.getTomcatList()) {
                new TomcatTool().getInfo(appVo);
                new FileTool().getFiles(appVo.getLogPath());
            }
            logger.warn("3、获取tomcat信息成功");
            logger.warn("4、获取jar信息");
            for (App appVo : clientConfig.getJarList()) {
                new JarTool().getInfo(appVo);
                new FileTool().getFiles(appVo.getLogPath());
            }
            logger.warn("4、获取jar信息成功");
            logger.warn("5、获取service信息");
            for (App appVo : clientConfig.getServiceList()) {
                ServiceVo serviceVo = new ServiceVo();
                serviceVo.setName(appVo.getName());
                serviceVo.setTime(DateUtils.dateTimeNow());
                serviceVo.setState(exe.exeServiceQuery(appVo.getServerName()));
            }
            logger.warn("5、获取service信息成功");
            logger.warn("6、获取bat信息");
            for (App appVo : clientConfig.getBatList()) {
                BatVo batVo = new BatVo();
                batVo.setName(appVo.getName());
                batVo.setTime(DateUtils.dateTimeNow());
                batVo.setState(exe.exeBatQuery(appVo.getServerName()));
            }
            logger.warn("6、获取bat信息成功");
            logger.warn("7、获取nginx配置信息");
            List<App> nginx = clientConfig.getBatList().stream().filter(appVo -> appVo.getServerName().equals("nginx")).collect(Collectors.toList());
            if (nginx.size() > 0) {
                nginxConfigEditor.getNginxConfig(nginx.get(0).getPath());
            }
            logger.warn("7、获取nginx配置信息成功");
        }
        logger.warn("初始化成功");
    }
}
